
const { baseUrl } = require("./index");
const login = require('./login')
/**
 * 配置request
 **/
const store = require("../storage");
const request = require("miniapp-request");

const md5 = require("md5");

//返回包含签名数据的对象
const sign = openid => {
    const timestamp = Date.parse(new Date()) / 1000;
    const token = md5(timestamp + "my9527azfzjkp" + openid);
    return {
        timestamp,
        openid,
        token
    };
};

//修改请求地址
request.defaults.baseUrl = baseUrl;
//接口加密
request.defaults.before = function (config) {
    return new Promise(function (resolve, reject) {

        const merge = (config, unionid) => {
            const data = Object.assign({}, sign(unionid), config.data)
            config.data = data
            return config
        }

        global.loginPromise.then(function (res) {
            console.log("===============接口加密");
            resolve(merge(config, res.openid));
        }).catch(function (e) {
            console.log("===============接口加密:网络异常");
            console.log(e);
            wx.showToast({
                title: '网络错误，请稍后重试!!',
                duration: 5000,
                icon: 'none'
            });
            reject(e);
        });

    });
};
//返回值拦截器
request.defaults.interceptor = function (res) {
    return res
}